﻿namespace PlugNT.Database
{
    using System;
    using System.Text;

    public class Sql
    {
        public static string GetPagingString(int topCount, int currPageCount, string tableName, string idName, string andWhere)
        {
            return GetPagingString(topCount, currPageCount, tableName, idName, "*", andWhere);
        }

        public static string GetPagingString(int topCount, int currPageCount, string tableName, string idName, string colNames, JoinInfo joinValue)
        {
            StringBuilder builder = new StringBuilder();
            string str = " order by a." + idName + " asc";
            builder.Append("select top " + topCount.ToString() + "  " + colNames + "," + joinValue.ColumnsString + "  from " + tableName + " as a " + joinValue.JoinTableString);
            if (currPageCount >= 1)
            {
                builder.Append(" where a." + idName + " >(");
                builder.Append("select max(" + idName + ") from (");
                string[] strArray = new string[] { "select top ", (topCount * currPageCount).ToString(), " ", idName, " from ", tableName, " where 1=1 ", joinValue.AndWhereToString() };
                builder.Append(string.Concat(strArray));
                builder.Append(") ast) " + joinValue.AndWhereToJoinString() + str);
            }
            else
            {
                builder.Append("  where 1=1 " + joinValue.AndWhereToJoinString() + str);
            }
            return builder.ToString();
        }

        public static string GetPagingString(int topCount, int currPageCount, string tableName, string idName, string colNames, string andWhere)
        {
            StringBuilder builder = new StringBuilder();
            string str = " order by " + idName + " asc";
            builder.Append("select top " + topCount.ToString() + "  " + colNames + "  from " + tableName);
            if (currPageCount >= 1)
            {
                builder.Append(" where " + idName + " >(");
                builder.Append("select max(" + idName + ") from (");
                string[] strArray = new string[] { "select top ", (topCount * currPageCount).ToString(), " ", idName, " from ", tableName, " where 1=1 ", andWhere };
                builder.Append(string.Concat(strArray));
                builder.Append(") ast) " + andWhere + str);
            }
            else
            {
                builder.Append("  where 1=1 " + andWhere + str);
            }
            return builder.ToString();
        }

        public static string GetPagingString(int topCount, int currPageCount, string tableName, string idName, string colNames, string orderNameList, string andWhere)
        {
            StringBuilder builder = new StringBuilder();
            string str = "";
            if (orderNameList.Trim() != "")
            {
                str = " order by " + orderNameList;
            }
            if (currPageCount >= 1)
            {
                builder.Append("select top " + topCount.ToString() + "  " + colNames + "  from " + tableName);
                builder.Append(" where " + idName + " not in (");
                string[] strArray = new string[] { "select top ", (topCount * currPageCount).ToString(), " ", idName, " from ", tableName, " where 1=1 ", andWhere, str };
                builder.Append(string.Concat(strArray));
                builder.Append(") " + andWhere + str);
            }
            else
            {
                builder.Append("select top " + topCount.ToString() + "  *  from " + tableName);
                builder.Append("  where 1=1 " + andWhere + str);
            }
            return builder.ToString();
        }

        public static string GetRePagingString(int topCount, int currPageCount, string tableName, string idName, string andWhere)
        {
            return GetRePagingString(topCount, currPageCount, tableName, idName, "*", andWhere);
        }

        public static string GetRePagingString(int topCount, int currPageCount, string tableName, string idName, string colNames, JoinInfo joinValue)
        {
            StringBuilder builder = new StringBuilder();
            string str = " order by " + idName + " desc";
            string str2 = " order by a." + idName + " desc";
            builder.Append("select top " + topCount.ToString() + "  " + colNames + "," + joinValue.ColumnsString + "  from " + tableName + " as a " + joinValue.JoinTableString);
            if (currPageCount >= 1)
            {
                builder.Append(" where a." + idName + " <(");
                builder.Append("select min(" + idName + ") from (");
                string[] strArray = new string[] { "select top ", (topCount * currPageCount).ToString(), " ", idName, " from ", tableName, " where 1=1 ", joinValue.AndWhereToString(), str };
                builder.Append(string.Concat(strArray));
                builder.Append(") ast) " + joinValue.AndWhereToJoinString() + str2);
            }
            else
            {
                builder.Append("  where 1=1 " + joinValue.AndWhereToJoinString() + str2);
            }
            return builder.ToString();
        }

        public static string GetRePagingString(int topCount, int currPageCount, string tableName, string idName, string colNames, string andWhere)
        {
            StringBuilder builder = new StringBuilder();
            string str = " order by " + idName + " desc";
            builder.Append("select top " + topCount.ToString() + "  " + colNames + "  from " + tableName);
            if (currPageCount >= 1)
            {
                builder.Append(" where " + idName + " <(");
                builder.Append("select min(" + idName + ") from (");
                string[] strArray = new string[] { "select top ", (topCount * currPageCount).ToString(), " ", idName, " from ", tableName, " where 1=1 ", andWhere, str };
                builder.Append(string.Concat(strArray));
                builder.Append(") ast) " + andWhere + str);
            }
            else
            {
                builder.Append("  where 1=1 " + andWhere + str);
            }
            return builder.ToString();
        }

        public static string Help()
        {
            return "mailto:354990393@qq.com";
        }
    }
}

